package main

import (
	"bufio"
	"fmt"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/unicode"
	"golang.org/x/text/transform"
	"io/ioutil"
	"net/http"
	"regexp"
)
type Profile struct{
	Name 		string
	Picture 	string
	UserUrl 	string
	Gender		string
	Local		string
	Age			int
	Education	string
	Marriage	string
	Height		int
	UserDesc	string
	Salary		string
}
type ParseResult struct {
	Url			string
	CityName	string
	ParFunc 	string
}


func main(){
	content :=getContent("https://www.chem960.com/company")
	cityList:=ParseCityList(content)
	for _,city:=range cityList{
		content=getContent(city.Url)
		companyList:=ParseCity(content)
		for _,company:=range companyList{
			content=getContent(company.Url)
			productList:=ParseCompany(content)
			fmt.Println(productList)
		}
	}
}
func determineEncode(r *bufio.Reader)encoding.Encoding{
	bytes,err:=r.Peek(1024)
	if err!= nil {
		return unicode.UTF8
	}
	e,_,_:=charset.DetermineEncoding(bytes,"")
	return e
}
func getContent(url string)[]byte{
	var content []byte
	client:=http.Client{}
	request,err:=http.NewRequest(http.MethodGet,url,nil)
	if err!=nil {
		panic(err)
	}
	request.Header.Add("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0")
	resp,err:=client.Do(request)
	if err!=nil {
		panic(err)
	}
	defer resp.Body.Close()
	if resp.StatusCode!=http.StatusOK{
		fmt.Printf("状态出错了:%d \n ",resp.StatusCode)
		return content
	}
	newReader:=bufio.NewReader(resp.Body)
	e:=determineEncode(newReader)
	utf8Reader:=transform.NewReader(newReader,e.NewDecoder())
	content, err =ioutil.ReadAll(utf8Reader)
	return content
}

func getUserContent(url string)[]byte{
	var content []byte
	request,err:=http.NewRequest(http.MethodGet,"https://album.zhenai.com/u/1054850585",nil)
	//request,err:=http.NewRequest(http.MethodGet,url,nil)
	if err!=nil {
		panic(err)
	}
	request.Header.Add("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0")
	request.Header.Add("cookie", "FSSBBIl1UgzbN7NO=5mT_ifV9p5UR1pOqpGOITannTMtvRfiV6ra_e9G9vbA_xZpfn00KGh8d92T1eizlBURGtIA6244G1PtMZjZwLna; sid=79a89e4d-9635-46f7-9f48-e67c66aa5ce2; Hm_lvt_2c8ad67df9e787ad29dbd54ee608f5d2=1634976785; ec=aDldHflq-1634976785256-aed2ad68164a1-410210704; _efmdata=VyKwok3L%2BgcahiO2knpEaiS2XaGp2nXeheNRbfm0DnArJp5MRJCFja4MzfNzMrc09W0qyEnbhHN49%2BA%2BvaTXDMbCTO1nonE33MZe3Tgqf%2BQ%3D; _exid=IxO2oJ9jZ4zMYi3ZleulXop%2Ba9PiPKWSQwsdVSFCXjNovLuYJh0feRs54LvT6hAj%2F3wu8MkMZjOXTDTa7Q6Rgg%3D%3D; Hm_lpvt_2c8ad67df9e787ad29dbd54ee608f5d2=1634978823; FSSBBIl1UgzbN7NP=53UHxbKmunRAqqqmZ.mMTbqUHysJxXiF0DyXtOZum5I6r0xCjaC0Lk9MWvCNVgtSBq.5_uWQK8X6_zGblhRatZv_GQHgHx64pAXideOujOaHBAYJ9Wn6Dp1X7i45COoVeNhrm5DJOUnZW6XBZeEWNtR5QGBWnlgoregBhl.1PosDUqESZc6KGwDhKucYkOLjivfp749o64n8z.zfRdQGsKYuxqTVg7QFukq9GsIYqedCifrLV8RPhRbR9kUIo1Cg9NahYmCQwmYjd2KY86KEyvOfIfqOOJHxgbzye8YVm6cY_ISFk3vm5gzRTm9Bj3tpY0")
	client:=http.Client{}
	resp,err:=client.Do(request)
	defer resp.Body.Close()
	if resp.StatusCode!=http.StatusOK{
		fmt.Printf("状态出错了:%d \n ",resp.StatusCode)
		return content
	}
	newReader:=bufio.NewReader(resp.Body)
	e:=determineEncode(newReader)
	utf8Reader:=transform.NewReader(newReader,e.NewDecoder())
	content, err =ioutil.ReadAll(utf8Reader)
	return content
}

func ParseCityList(content []byte) ([]ParseResult){
	var result  []ParseResult
	re:=regexp.MustCompile(`<a href="//(www.chem960.com/company/companylist_area_[0-9a-z]+\.shtml)" class="layui-col-xs2">([^(]+)\([^)]+\)</a>`)
	//re:=regexp.MustCompile(`<a href="(http://www.zhenai.com/zhenghun/[0-9a-z]+)" [^>]*>([^<]+)</a>`)
	matches:=re.FindAllSubmatch(content,-1)
	for _,m:=range matches{
		result =append(result,ParseResult{Url:"https://"+string(m[1]),CityName:string(m[2]),ParFunc: "ParseCity"})
	}
	return result
}
func ParseCity(content []byte) []ParseResult{
	var result []ParseResult
	re:=regexp.MustCompile(` <a href="//(www.chem960.com/company/[0-9a-z]+/)" target="_blank" class="blue_u" title="([^\"]*)">`)
	//re:=regexp.MustCompile(`<a href="(http://www.zhenai.com/zhenghun/[0-9a-z]+)" [^>]*>([^<]+)</a>`)
	matches:=re.FindAllSubmatch(content,-1)
	for _,m:=range matches{
		result =append(result,ParseResult{Url:"https://"+string(m[1]),CityName:string(m[2]),ParFunc: "ParseCompany"})
	}
	return result
}

func ParseCompany(content []byte) []ParseResult{
	var result []ParseResult
	re:=regexp.MustCompile(` <a href="//(www.chem960.com/company/[0-9a-z]+/)" target="_blank" class="blue_u" title="([^\"]*)">`)
	//re:=regexp.MustCompile(`<a href="(http://www.zhenai.com/zhenghun/[0-9a-z]+)" [^>]*>([^<]+)</a>`)
	matches:=re.FindAllSubmatch(content,-1)
	for _,m:=range matches{
		result =append(result,ParseResult{Url:"https://"+string(m[1]),CityName:string(m[2]),ParFunc: "ParseCompany"})
	}
	return result
}


//func PrintUserList(content []byte){
//	//fmt.Printf("%s \n",content)
//
//	//re:=regexp.MustCompile(`<a href="(http://album.zhenai.com/u/[0-9]+)"[^>]*>([^<]+)</a>`)
//	re:=regexp.MustCompile(`<div class="list-item"><div class="photo"><a href="[^"]+" target="_blank"><img src="([^\?]+)\?[^"]+" alt="[^"]+"></a></div> <div class="content"><table><tbody><tr><th><a href="([^"]+)" target="_blank">([^<]+)</a></th></tr> <tr><td width="180"><span class="grayL">性别：</span>([^<]+)</td> <td><span class="grayL">居住地：</span>([^<]+)</td></tr> <tr><td width="180"><span class="grayL">年龄：</span>([^<]+)</td> <td><span class="grayL">学   历：</span>([^<]+)</td> </tr> <tr><td width="180"><span class="grayL">婚况：</span>([^<]+)</td> <td width="180"><span class="grayL">身   高：</span>([^<]+)</td></tr></tbody></table> <div class="introduce">([^<]+)</div>`)
//
//	womenS:=re.FindAllSubmatch(content,-1)
//	for _,m:=range womenS{
//		var person Profile
//		person.Picture	=string(m[1])
//		person.UserUrl	=string(m[2])
//		person.Name		=string(m[3])
//		person.Gender	=string(m[4])
//		person.Local	=string(m[5])
//		age,err:=strconv.Atoi(string(m[6]))
//		if err!=nil {
//			person.Age=0
//		}else{
//			person.Age=age
//		}
//		person.Education	=string(m[7])
//		person.Marriage		=string(m[8])
//		height,err:=strconv.Atoi(string(m[9]))
//		if err!=nil {
//			person.Height=0
//		}else{
//			person.Height=height
//		}
//		person.UserDesc		=string(m[10])
//		fmt.Println(person)
//	}
//	re2:=regexp.MustCompile(`<div class="list-item"><div class="photo"><a href="[^"]+" target="_blank"><img src="([^\?]+)\?[^"]+" alt="[^"]+"></a></div> <div class="content"><table><tbody><tr><th><a href="([^"]+)" target="_blank">([^<]+)</a></th></tr> <tr><td width="180"><span class="grayL">性别：</span>([^<]+)</td> <td><span class="grayL">居住地：</span>([^<]+)</td></tr> <tr><td width="180"><span class="grayL">年龄：</span>([^<]+)</td>  <td><span class="grayL">月   薪：</span>([^<]+)</td></tr> <tr><td width="180"><span class="grayL">婚况：</span>([^<]+)</td> <td width="180"><span class="grayL">身   高：</span>([^<]+)</td></tr></tbody></table> <div class="introduce">([^<]+)</div>`)
//	manS:=re2.FindAllSubmatch(content,-1)
//	for _,m:=range manS{
//		var person Profile
//		person.Picture	=string(m[1])
//		person.UserUrl	=string(m[2])
//		person.Name		=string(m[3])
//		person.Gender	=string(m[4])
//		person.Local	=string(m[5])
//		age,err:=strconv.Atoi(string(m[6]))
//		if err!=nil {
//			person.Age=0
//		}else{
//			person.Age=age
//		}
//		person.Salary	=string(m[7])
//		person.Marriage		=string(m[8])
//		height,err:=strconv.Atoi(string(m[9]))
//		if err!=nil {
//			person.Height=0
//		}else{
//			person.Height=height
//		}
//		person.UserDesc		=string(m[10])
//		fmt.Println(person)
//	}
//	//fmt.Println(matches)
//	//panic(123)
//}
//
//func GetUserInfo(content []byte){
//	fmt.Printf("%s\n",content)
//	userInfo:=Profile{}
//	nameReg:=regexp.MustCompile(`<h1 class="nickName" [^>]*>([^<]+)</h1>`)
//	matchesName:=nameReg.FindSubmatch(content)
//	if len(matchesName)>0 {
//		userInfo.Name = string(matchesName[1])
//	}
//	GenderReg:=regexp.MustCompile(`"genderString":"([^"]+)"`)
//	genderMatch:=GenderReg.FindSubmatch(content)
//	if len(genderMatch)>0 {
//		userInfo.Gender = string(genderMatch[1])
//	}
//	AgeReg:=regexp.MustCompile(`<div class="m-btn purple" [^>]*>([\d]+)岁</div>`)
//	ageMatch:=AgeReg.FindSubmatch(content)
//	if len(ageMatch)>0 {
//		age,err := strconv.Atoi(string(ageMatch[1]))
//		if err == nil {
//			userInfo.Age = age
//		}
//	}
//	HeightReg:=regexp.MustCompile(`<div class="m-btn purple" [^>]*>([\d]+)cm</div>`)
//	heightMatch:=HeightReg.FindSubmatch(content)
//	if len(heightMatch)>0 {
//		height,err := strconv.Atoi(string(heightMatch[1]))
//		if err == nil {
//			userInfo.Height = height
//		}
//	}
//	WeightReg:=regexp.MustCompile(`<div class="m-btn purple" [^>]*>([\d]+)kg</div>`)
//	weightMatch:=WeightReg.FindSubmatch(content)
//	if len(weightMatch)>0 {
//		weight,err := strconv.Atoi(string(weightMatch[1]))
//		if err == nil {
//			userInfo.Weight = weight
//		}
//	}
//	IncomeReg:=regexp.MustCompile(`<div class="m-btn purple" [^>]*>月收入:([^<]+)</div>`)
//	incomeMatch:=IncomeReg.FindSubmatch(content)
//	if len(incomeMatch)>0 {
//		userInfo.Income =string(incomeMatch[1])
//	}
//
//	MarriageReg:=regexp.MustCompile(`<div class="m-btn purple" [^>]*>([离异|未婚|已婚]+)</div>`)
//	marriageMatch:=MarriageReg.FindSubmatch(content)
//	if len(marriageMatch)>0 {
//		userInfo.Marriage =string(marriageMatch[1])
//	}
//
//	EducationReg:=regexp.MustCompile(`<div class="m-btn purple" [^>]*>([大学本科|高中及以下|大专|中专]+)</div>`)
//	eduMatch:=EducationReg.FindSubmatch(content)
//	if len(eduMatch)>0 {
//		userInfo.Education =string(eduMatch[1])
//	}
//
//	OccupationReg:=regexp.MustCompile(`<div class="m-btn" [^>]*>工作地:([^<]+)</div>`)
//	occupation:=OccupationReg.FindSubmatch(content)
//	if len(occupation)>0 {
//		userInfo.Occupation =string(occupation[1])
//	}
//
//	HokouReg:=regexp.MustCompile(`<div class="m-btn pink" [^>]*>籍贯:([^<]+)</div>`)
//	hokou:=HokouReg.FindSubmatch(content)
//	if len(hokou)>0 {
//		userInfo.Hokou =string(hokou[1])
//	}
//
//	XinzuoReg:=regexp.MustCompile(`<div class="m-btn purple" data-v-8b1eac0c>([^>]+座[^)]+\))</div>`)
//	xinzuo:=XinzuoReg.FindSubmatch(content)
//	if len(xinzuo)>0 {
//		userInfo.Xinzuo =string(xinzuo[1])
//	}
//
//
//	HouseReg:=regexp.MustCompile(`<div class="m-btn pink" [^>]*>([已购房|租房]+)</div>`)
//	house:=HouseReg.FindSubmatch(content)
//	if len(house)>0 {
//		userInfo.House =string(house[1])
//	}else{
//		userInfo.House =""
//	}
//
//	CarReg:=regexp.MustCompile(`<div class="m-btn pink" [^>]*>([已买车|未买车]+)</div>`)
//	car:=CarReg.FindSubmatch(content)
//	if len(car)>0 {
//		userInfo.Car =string(car[1])
//	}else{
//		userInfo.Car =""
//	}
//	fmt.Println(userInfo)
//	panic("*****")
//}
